﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>

<?php //check if svarstymasisvykaid set
	if (!isset($_GET['svarstymasisvykaid'])) {
		$svarstymasisvykaid = 0;  
		}else{
		$svarstymasisvykaid = ($_GET['svarstymasisvykaid']);
		//gaunam paskatinimo detales
		$query = "SELECT * 
			FROM `svarstymasisvyka`
			WHERE svarstymasisvykaid=";
		$query .=($_GET['svarstymasisvykaid']);
		$result_set = mysql_query($query, $connection);
		$sabotage = mysql_num_rows($result_set);
		$getisvyka= mysql_fetch_array($result_set);
		//tikrinam ar yra toks paskatinimas ir ar jis priklauso tam klientui
		if(($sabotage==0) or (($getisvyka['userid'])!=($_GET['userid']))){echo "<h1>Sabotage<h1>";};
		
		};
 ?> 
 
<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

<?php //Gaunam Pagrindinius kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`, `lovos`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= "AND `user`.lovosid = `lovos`.lovosid";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);

?>

 <?php //procesinam ka gaunam is post
 
if (isset($_POST['submit'])) {
	
	if($svarstymasisvykaid==0){
		//naujo pradzia
		$sql = "INSERT INTO `svarstymasisvyka` (`userid`, `svarstdata`, `buvogeras`, `burys`";
		$sql .= ") VALUES (";
		$sql .= $subject['userid'] . ", ";
		$sql .= "'".$_POST['svarstymodata']."'" . ", ";
		$sql .= $_POST['geras'] . ", ";
		$sql .= $subject['burys'];
		$sql .= ");";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				 $to = "summary.php?userid=" . $_GET['userid'];
				 redirect_to($to);
				// Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//naujo pabaiga
	}else{	
		//modifikavimo pradzia
		$sql = "UPDATE `svarstymasisvyka`";
		//$sql = "INSERT INTO `svarstymasisvyka` (`userid`, `svarstdata`, `buvogeras`, `burys`";
		$sql .= " SET";
		$sql .= " `userid`=";
		$sql .= $subject['userid'] . ", ";
		$sql .= " `svarstdata`=";
		$sql .= "'".$_POST['svarstymodata']."'" . ", ";
		$sql .= " `buvogeras`=";
		$sql .= $_POST['geras'] . ", ";
		$sql .= " `burys`=";
		$sql .= $subject['burys'];
		$sql .= " WHERE `svarstymasisvykaid`=";
		$sql .= $_GET['svarstymasisvykaid'];
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
				 $to = "form_preprot.php?svarstdata=" . $_POST['svarstymodata'];
				 redirect_to($to);
				// Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//modifikavimo pabaiga
	};	
};
	
  

 ?>


			
					<article>
						<h2><?php 
							if($svarstymasisvykaid !=0){
								echo "Išvykos redagavimas";
							}else{
								echo "Išvykos formavimas";
							};
						
						?></h2>
	<?php echo $message."</br>";?>
	<?php //rodomi pagrindiniai kliento duomenys

	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
	$tau_dabar = $subject['tautybeid'];
	$pil_dabar = $subject['pilietybeid'];
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form action="form_isvyka.php?userid=<?php echo ($_GET['userid']);if($svarstymasisvykaid !=0){echo "&svarstymasisvykaid=".$svarstymasisvykaid;};?>" method="post" name="submit">
  
  </br>
  Svarstymo data: <input name="svarstymodata" type="date" value="<?php 
							if($svarstymasisvykaid !=0){
								echo ($getisvyka['svarstdata']);
							}else {echo date("Y-m-j");};
						
						?>" placeholder="1900-12-31" required/>
 <br>
  Buvo geras: 
  <input name="geras" type="checkbox" id="geras" value="1" <?php 
							if($svarstymasisvykaid !=0){
								if (($getisvyka['buvogeras']) ==1){
									echo "checked";};
							};
						
						?> />
  <label for="geras"></label>
<br>
<p>
  
  
<input name="submit" type="submit" value="SubmiNt" />
</form>

			</article>
            
<?php require("includes/footer.php"); ?>
